package com.musemun.controller;

import com.google.zxing.WriterException;
import com.musemun.exception.MyDefineException;
import com.musemun.util.TwoCodeUtil;
import io.swagger.annotations.Api;
import org.apache.poi.util.IOUtils;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.concurrent.ExecutorService;

/**
 * @description:
 *             winner 的测试类 无任何意义
 * @author winner
 * @date 2020/4/14
 */
@RestController
@Api(tags = "这是winner的一些测试方法")
@RequestMapping("/main")
public class MainController extends WebMvcAutoConfiguration {
    @Resource(name = "getThreadPool")
    ExecutorService executorService;
    @PostMapping("/upload")
    public void upload(String directoryName, MultipartFile[] uploads) throws IOException {
        long start=System.currentTimeMillis();
//        return  UploadFileUtils.upload(directoryName,uploads,executorService);
    }

//    @GetMapping("/Error")
//    public String myExceptionText() throws MyDefineException {
////        throw new MyDefineException(404,"abn");
//    }

    @GetMapping("test")
    public String test(HttpServletResponse response) throws WriterException, IOException {
        return TwoCodeUtil.generateCodeBufferImage("https://www.baidu.com/");
    }

    @RequestMapping("/downloadFileAction")
    public void downloadFileAction(HttpServletRequest request, HttpServletResponse response) {

        response.setCharacterEncoding(request.getCharacterEncoding());
        response.setContentType("application/octet-stream");
        FileInputStream fis = null;
        try {
            File file = new File("C:\\config.ini");
            fis = new FileInputStream(file);
            response.setHeader("Content-Disposition", "attachment; filename=" + file.getName());
            IOUtils.copy(fis, response.getOutputStream());
            response.flushBuffer();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
